﻿
using System.Web;
using System.Web.Mvc;

namespace MNH.WebApps.Common.Helpers.JqGrid
{
    public class GridModelBinder : IModelBinder
    {
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            try
            {
                HttpRequestBase request = controllerContext.HttpContext.Request;
                return (object)new GridSettings()
                {
                    IsSearch = bool.Parse(request["_search"] ?? "false"),
                    PageIndex = int.Parse(request["page"] ?? "1"),
                    PageSize = int.Parse(request["rows"] ?? "10"),
                    SortColumn = (request["sidx"] ?? ""),
                    SortOrder = (request["sord"] ?? "asc"),
                    SearchField = (request["searchField"] ?? string.Empty),
                    SearchOper = (request["searchOper"] ?? string.Empty),
                    Filters = (request["filters"] ?? string.Empty),
                    Where = Filter.Create(request["filters"] ?? ""),
                    Operation = (request["oper"] ?? string.Empty)
                };
            }
            catch
            {
                return (object)null;
            }
        }
    }
}
